package com.lc.zpyh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lc.zpyh.R;
import com.lc.zpyh.bean.BannerDataBean;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自定义布局,多个不同UI切换
 */
public class MultipleTypesAdapter extends BannerAdapter<BannerDataBean, RecyclerView.ViewHolder> {
    boolean PlayFlag = false;
    private Context context;
    private SparseArray<RecyclerView.ViewHolder> mVHMap = new SparseArray<>();
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClicked(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.mOnItemClickListener = clickListener;
    }
    public MultipleTypesAdapter(Context context, List<BannerDataBean> mDatas) {
        super(mDatas);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
            case 2:
                return new VideoHolder(BannerUtils.getView(parent, R.layout.banner_video),mOnItemClickListener);
            case 3:
                return new TitleHolder(BannerUtils.getView(parent, R.layout.banner_title));
        }
        return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
    }

    @Override
    public int getItemViewType(int position) {
        //先取得真实的position,在获取实体
//        return getData(getRealPosition(position)).viewType;
        //直接获取真实的实体
        return getRealData(position).viewType;
        //或者自己直接去操作集合
//        return mDatas.get(getRealPosition(position)).viewType;
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, BannerDataBean data, int position, int size) {
        int viewType = holder.getItemViewType();
        switch (viewType) {
            case 1:
                ImageHolder imageHolder = (ImageHolder) holder;
                mVHMap.append(position,imageHolder);
                Glide.with(context)
                    .load(data.imageUrl).apply(new RequestOptions().placeholder(R.mipmap.zhanweiicon01).error(R.mipmap.zhanweiicon01).centerCrop())
                    .into((ImageView) imageHolder.imageView);

                break;
            case 2:
                VideoHolder videoHolder = (VideoHolder) holder;
                mVHMap.append(position,videoHolder);
//                TCControllerWindow controllerWindow =  videoHolder.player.getTCControllerWindow();
//                ImageView askLayer = controllerWindow.getAskLayer();//封面图
//                askLayer.setVisibility(View.VISIBLE);
//                videoHolder.player.hideFullscreen();
//                videoHolder.video_paly.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        SuperPlayerModel model = new SuperPlayerModel();
//
//                        model.url = data.imageUrl;
//                        videoHolder.player.playWithModel(model,(int) videoHolder.player.getTCControllerWindow().getmProgress());
//
//                    }
//                });
//                SuperPlayerModel model = new SuperPlayerModel();
//
//                model.url = data.imageUrl;
//                Log.e("@@@", "onBindView: "+ videoHolder.player.getTCControllerWindow().getmProgress());
//                float percentage = ((float)  videoHolder.player.getTCControllerWindow().getmProgress() / (float)  videoHolder.player.getTCControllerWindow().getmDuration());
//                int progress = Math.round(percentage * 100);
//                Log.e("@@@@", "onBindView: "+ progress);
//                if (!PlayFlag){
//                    videoHolder.player.playWithModel(model,(int) videoHolder.player.getTCControllerWindow().getmProgress());
//                    videoHolder.player.hideFullscreen();
//                    PlayFlag = true;
//                }
//                videoHolder.player.setOnSeekBarChangeListener(new SuperPlayerView.OnSeekBarChangeListener() {
//                    @Override
//                    public void onProgressChanged(TCPointSeekBar seekBar, int progress, boolean fromUser, boolean mIsOnDrag) {
//                        if (progress == 100){
//                            videoHolder.player.setJumpCurrent(0);
//                        }
//                    }
//                });

//                videoHolder.player.setUp(data.imageUrl, true, null);
//                videoHolder.player.getBackButton().setVisibility(View.GONE);
                //增加封面
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                imageView.setImageResource(R.drawable.image4);
//                videoHolder.player.setThumbImageView(imageView);
//                videoHolder.player.startPlayLogic();
                break;
            case 3:
                TitleHolder titleHolder = (TitleHolder) holder;
                mVHMap.append(position,titleHolder);
                titleHolder.title.setText(data.title);
                titleHolder.title.setBackgroundColor(Color.parseColor(BannerDataBean.getRandColor()));
                break;
        }
    }

    public SparseArray<RecyclerView.ViewHolder> getVHMap() {
        return mVHMap;
    }
    public class ImageTitleHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView title;

        public ImageTitleHolder(@NonNull View view) {
            super(view);
            imageView = view.findViewById(R.id.image);
            title = view.findViewById(R.id.bannerTitle);
        }
    }
    public class ImageHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public ImageHolder(@NonNull View view) {
            super(view);
            this.imageView = (ImageView) view;
        }
    }
    public class TitleHolder extends RecyclerView.ViewHolder {
        public TextView title;

        public TitleHolder(@NonNull View view) {
            super(view);
            title = view.findViewById(R.id.bannerTitle);
        }
    }
    public class VideoHolder extends RecyclerView.ViewHolder {
//        public SuperPlayerView player;
//        public ImageView video_paly;
        public VideoHolder(@NonNull View view, final OnItemClickListener onClickListener) {
            super(view);
//            player = view.findViewById(R.id.player);
//            video_paly= view.findViewById(R.id.video_paly);
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (onClickListener != null) {
//                        int position = getAdapterPosition();
//                        //确保position值有效
//                        if (position != RecyclerView.NO_POSITION) {
//                            onClickListener.onItemClicked(view, position);
//                        }
//                    }
//                }
//            });

        }
    }

}
